import validHtml from 'ngtemplate!html!../../../tpls/dialog/valid.dialog.html';

class ChipEditController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, ChipService, getChipDetail, iRunner){

    	var vm = this;

    	vm.$rootScope    = $rootScope;
    	vm.$cookieStore  = $cookieStore;
    	vm.$timeout 	 = $timeout;
    	vm.$state   	 = $state;
    	vm.ngDialog 	 = ngDialog;
    	vm.dialogService = dialogService;
    	vm.ChipService   = ChipService;
    	vm.getChipDetail = getChipDetail;
    	vm.iRunner 	     = iRunner;

        // 芯片厂商列表
        vm.chip_maker_list = vm.iRunner.chipMakerConfig;

    	// 表单数据初始化
        vm.chip = {
            bib: '',
            chip_no: '',
            chip_maker: '',
            chip_mac: ''
        }

        // 获取芯片详情
        if (vm.getChipDetail.code==0) {

        	var Detail = vm.getChipDetail.data;

            vm.chip.bib        = Detail.bib;
            vm.chip.chip_no    = Detail.chip_no;
            vm.chip.chip_mac   = Detail.chip_mac;

            // 遍历芯片厂商列表, 选择对应的chip_maker渲染页面
            angular.forEach(vm.chip_maker_list, function(value, index) {
                if (value.name == Detail.chip_maker) {
                    vm.chip.chip_maker = value;
                }
            });

        }

        // 表单提交
        vm.submitForm = function(isValid) {

            // 验证失败, 提示用户
            if (!isValid) {
                
                if (!vm.chip.chip_no) {
                    $.blink(angular.element(document.getElementsByName('chipNo')), 5, 100)
                } else if (!vm.chip.chip_maker) {
                    $.blink(angular.element(document.getElementsByName('chipMaker')), 5, 100)
                } else if (!vm.chip.chip_mac) {
                    $.blink(angular.element(document.getElementsByName('chipMac')), 5, 100)
                }
                
            }

            // 验证通过
            if (isValid) {
                
                // 拼接POST数据格式
                var params = {
                    bib: vm.chip.bib,
                    chip_no: vm.chip.chip_no,
                    chip_maker: vm.chip.chip_maker.name,
                    chip_mac: vm.chip.chip_mac
                }

                console.log(params)

                // 更新芯片详情
                vm.ChipService.postChipUpdate(params, $cookieStore.get('access_token'), $state.params.id)
                .then(function(data) {

                    // 成功调用
                    data.code==0&&vm.dialogService.success('更新成功', 'dashboard.race.chiplist');

                    // 调用失败, 提示用户
                    (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                    // 登录信息已过期, 请重新登录
                    data.code==-1001&&vm.dialogService.error(data.message, true);

                    console.log(data)
                }, function(data) {
                    console.log('error')
                });
            }

            return false;
        }

        // 删除芯片信息
        vm.deleteItems = function(id) {

            // 模态框显示文本
            $rootScope.common = {
                text : '是否删除芯片信息'
            }

            vm.ngDialog.open({
                template: validHtml,
                className: 'ngdialog-theme-default ngdialog-alert',
                scope: $scope
            });

            // 提交表单
            $rootScope.submitDelForm = function(isValid) {

                // 验证失败, 提示用户
                if (!isValid) {
                    vm.dialogService.error();
                }

                // 验证通过
                if (isValid) {

                    // 关闭模态框
                    vm.ngDialog.close();

                    vm.ChipService.postChipDel({id: id}, vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
                    .then(function(data) {
                        
                        // 成功调用
                        data.code==0&&vm.dialogService.success('芯片信息删除成功', 'dashboard.race.chiplist');

                        // 调用失败, 提示用户
                        (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                        // 登录信息已过期, 请重新登录
                        data.code==-1001&&vm.dialogService.error(data.message, true);

                        console.log(data)
                    }, function(data) {
                        console.log('error')
                    });
                }

                return false;
            }
        }

    }
}

ChipEditController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'ChipService', 'getChipDetail', 'iRunner']; 

export default ChipEditController;